package body Menu is

    function New_Menu (Title : String) return T_Menu is
    begin
        return T_Menu'(
            Items => Menu_Vector.Cree_SListe,
            Title => To_Unbounded_String(Title)
            );
    end New_Menu;

    procedure New_Item (
        M : in out T_Menu;
        Key : in Character;
        Text : in String;
        Callback : in Callback_Procedure
        ) is   
    begin
        Menu_Vector.Ajout_Fin(M.Items, T_Item'(Key, To_Unbounded_String(Text), Callback));
    end New_Item;
    
    procedure Show (M : in T_Menu) is
        procedure Put_Item (Item : in T_Item) is
        begin
            Put(Item.Key);
            Put_Line(". " & To_String(Item.Text));
        end Put_Item;
        
        procedure Put_Items is new Menu_Vector.Foreach(Put_Item);
        
        Key : Character;
        
        function Compare_Keys (Item : T_Item) return Boolean is
        begin
            if Is_Letter(Key) then
                -- Comparaison insensible a la casse
                return To_Upper(Item.Key) = To_Upper(Key);
            else
                return Item.Key = Key;
            end if;
        end Compare_Keys;
        
        function Find_Item is new Menu_Vector.Cherche(Compare_Keys);
        
        Item_Vector : Vector;
        Callback : Callback_Procedure;
    begin
        loop
            New_Line;
            Put_Line("====== " & To_String(M.Title) & " ======");
            Put_Items(M.Items); New_Line;
            
            loop
                Put("? "); Get(Key);
                Item_Vector := Find_Item(M.Items);              
                if Menu_Vector.Est_Vide(Item_Vector) then
                    Put_Line("Saisie invalide !");
                end if;
                exit when not Menu_Vector.Est_Vide(Item_Vector);
            end loop;
                
            Callback := Menu_Vector.GetCourant(Item_Vector).Callback;
        
            -- Appeler la procedure associee a l'item
            if (Callback /= null) then
                Callback.all;
            end if;
            exit when Callback = null;
        end loop;
    end Show;
        
end Menu;
    